package com.manage.project.biz.controller;

import java.util.List;
import java.util.Map;

import com.manage.common.utils.DateUtils;
import com.manage.project.biz.domain.BizQitasheshidingqijiancha;
import com.manage.project.biz.domain.BizQitasheshijingchangjiancha;
import com.manage.project.biz.domain.BizQitasheshizhuanxiangjiancha;
import com.manage.project.biz.mapper.BizGongluMapper;
import com.manage.project.biz.service.IBizQitasheshidingqijianchaService;
import com.manage.project.biz.service.IBizQitasheshijingchangjianchaService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.manage.framework.aspectj.lang.annotation.Log;
import com.manage.framework.aspectj.lang.enums.BusinessType;
import com.manage.project.biz.domain.BizQitasheshijishupingding;
import com.manage.project.biz.service.IBizQitasheshijishupingdingService;
import com.manage.framework.web.controller.BaseController;
import com.manage.framework.web.domain.AjaxResult;
import com.manage.common.utils.poi.ExcelUtil;
import com.manage.framework.web.page.TableDataInfo;

/**
 Controller
 *
 * @author liutao
 *
 */
@RestController
@RequestMapping("/biz/qitasheshijishupingding")
public class BizQitasheshijishupingdingController extends BaseController
{
    @Autowired
    private IBizQitasheshijishupingdingService bizQitasheshijishupingdingService;

    @Autowired
    private IBizQitasheshijingchangjianchaService bizQitasheshijingchangjianchaService;
    @Autowired
    private IBizQitasheshidingqijianchaService bizQitasheshidingqijianchaService;
    @Autowired
    private BizGongluMapper bizGongluMapper;

    /**
     * 查询其他设施技术评定列表
     */
//    @PreAuthorize("@ss.hasPermi('biz:qitasheshijishupingding:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizQitasheshijishupingding bizQitasheshijishupingding)
    {
        startPage();
        List<BizQitasheshijishupingding> list = bizQitasheshijishupingdingService.selectBizQitasheshijishupingdingList(bizQitasheshijishupingding);
        return getDataTable(list);
    }

    /**
     * 导出其他设施技术评定列表
     */
//    @PreAuthorize("@ss.hasPermi('biz:qitasheshijishupingding:export')")
    @Log(title = "其他设施技术评定", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BizQitasheshijishupingding bizQitasheshijishupingding)
    {
        List<BizQitasheshijishupingding> list = bizQitasheshijishupingdingService.selectBizQitasheshijishupingdingList(bizQitasheshijishupingding);
        ExcelUtil<BizQitasheshijishupingding> util = new ExcelUtil<BizQitasheshijishupingding>(BizQitasheshijishupingding.class);
        return util.exportExcel(list, "其他设施技术评定数据");
    }

    /**
     * 获取其他设施技术评定详细信息
     */
//    @PreAuthorize("@ss.hasPermi('biz:qitasheshijishupingding:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bizQitasheshijishupingdingService.selectBizQitasheshijishupingdingById(id));
    }

    /**
     * 新增其他设施技术评定
     */
    // @PreAuthorize("@ss.hasPermi('biz:qitasheshijishupingding:add')")
    @Log(title = "其他设施技术评定", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizQitasheshijishupingding bizQitasheshijishupingding)
    {

        float qtci = 0;
        String lev="";

        if(bizQitasheshijishupingding.getField12().equals("经常检查")){
            System.out.println("进入方法");
            BizQitasheshijingchangjiancha  bizQitasheshijingchangjiancha = new BizQitasheshijingchangjiancha();
            bizQitasheshijingchangjiancha.setField1(bizQitasheshijishupingding.getField11());
            List<BizQitasheshijingchangjiancha> bizQitasheshijingchangjianchaList=bizQitasheshijingchangjianchaService.selectBizQitasheshijingchangjianchaList(bizQitasheshijingchangjiancha);

            //本次评定日期
            //List<Map<String,Object>> maps =  bizGongluMapper.getMaplist("SELECT field12,field14 FROM biz_qitasheshijingchangjiancha");
            //权重总和
            double wh = 100;
            System.out.println("这是第一步");
//        for (BizQitasheshijingchangjiancha qitasheshijingchangjiancha: bizQitasheshijingchangjianchaList) {
//            if(bizQitasheshijingchangjiancha.getField1().equals(bizQitasheshijishupingding.getField11())) {
//                if (qitasheshijingchangjiancha.getField12().equals("电缆沟")) {
//                    wh += 10;
//                    System.out.println("进入权重计算循环");
//                } else if (qitasheshijingchangjiancha.getField12().equals("设备洞室")) {
//                    wh += 10;
//                } else if (qitasheshijingchangjiancha.getField12().equals("洞外联络通道")) {
//                    wh += 9;
//                } else if (qitasheshijingchangjiancha.getField12().equals("洞口限高门架")) {
//                    wh += 14;
//                } else if (qitasheshijingchangjiancha.getField12().equals("洞口绿化")) {
//                    wh += 3;
//                } else if (qitasheshijingchangjiancha.getField12().equals("消音设施")) {
//                    wh += 3;
//                } else if (qitasheshijingchangjiancha.getField12().equals("减光设施")) {
//                    wh += 10;
//                } else if (qitasheshijingchangjiancha.getField12().equals("污水处理设施")) {
//                    wh += 4;
//                } else if (qitasheshijingchangjiancha.getField12().equals("洞口雕塑、隧道铭牌")) {
//                    wh += 2;
//                } else if (qitasheshijingchangjiancha.getField12().equals("房屋设施")) {
//                    wh += 35;
//                }
//            }
//        }
            //各项得分总和
            double dfzh = 0;
            System.out.println("这是第二步");
            for (BizQitasheshijingchangjiancha qitasheshijingchangjiancha: bizQitasheshijingchangjianchaList) {
                if(bizQitasheshijingchangjiancha.getField1().equals(bizQitasheshijishupingding.getField11())) {
                    double qtcii = Integer.parseInt(qitasheshijingchangjiancha.getField14());   //分项设施状况值
                    if (qitasheshijingchangjiancha.getField12().equals("电缆沟")) {
                        dfzh += (100 * qtcii)/ wh;
                        System.out.println(dfzh);
                    } else if (qitasheshijingchangjiancha.getField12().equals("设备洞室")) {
                        dfzh += (100 * qtcii )/ wh;
                        System.out.println(dfzh);
                    } else if (qitasheshijingchangjiancha.getField12().equals("洞外联络通道")) {
                        dfzh += (90 * qtcii)/ wh ;
                        System.out.println(dfzh);
                    } else if (qitasheshijingchangjiancha.getField12().equals("洞口限高门架")) {
                        dfzh += 140* qtcii/ wh;
                    } else if (qitasheshijingchangjiancha.getField12().equals("洞口绿化")) {
                        dfzh += 30  * qtcii/ wh;
                    } else if (qitasheshijingchangjiancha.getField12().equals("消音设施")) {
                        dfzh += 30  * qtcii/ wh;
                    } else if (qitasheshijingchangjiancha.getField12().equals("减光设施")) {
                        dfzh += 100  * qtcii/ wh;
                    } else if (qitasheshijingchangjiancha.getField12().equals("污水处理设施")) {
                        dfzh += 40 * qtcii/ wh;
                    } else if (qitasheshijingchangjiancha.getField12().equals("洞口雕塑、隧道铭牌")) {
                        dfzh += 20  * qtcii/ wh;
                    } else if (qitasheshijingchangjiancha.getField12().equals("房屋设施")) {
                        dfzh += 350 * qtcii / wh;
                    }
                }
            }
            System.out.println("++++++++++++++++"+dfzh);
            //计算QTCI
            qtci =  (float)(100*(1-dfzh*0.05));
            //技术评定
            lev  = "无";
            if(qtci>=70){ lev = "1类";
            }else if(qtci>=40&& qtci<70){ lev = "2类";
            }else if(qtci<40){ lev = "3类";
            }

        }

        else{             //定期检查技术评定
            System.out.println("进入方法");
            BizQitasheshidingqijiancha  bizQitasheshidingqijiancha = new BizQitasheshidingqijiancha();
            bizQitasheshidingqijiancha.setField1(bizQitasheshijishupingding.getField11());
            List<BizQitasheshidingqijiancha> bizQitasheshidingqijianchaList=bizQitasheshidingqijianchaService.selectBizQitasheshidingqijianchaList(bizQitasheshidingqijiancha);

            //本次评定日期
            //List<Map<String,Object>> maps =  bizGongluMapper.getMaplist("SELECT field12,field14 FROM biz_qitasheshijingchangjiancha");
            //权重总和
            double wh = 100;
            System.out.println("这是第一步");
//        for (BizQitasheshijingchangjiancha qitasheshijingchangjiancha: bizQitasheshijingchangjianchaList) {
//            if(bizQitasheshijingchangjiancha.getField1().equals(bizQitasheshijishupingding.getField11())) {
//                if (qitasheshijingchangjiancha.getField12().equals("电缆沟")) {
//                    wh += 10;
//                    System.out.println("进入权重计算循环");
//                } else if (qitasheshijingchangjiancha.getField12().equals("设备洞室")) {
//                    wh += 10;
//                } else if (qitasheshijingchangjiancha.getField12().equals("洞外联络通道")) {
//                    wh += 9;
//                } else if (qitasheshijingchangjiancha.getField12().equals("洞口限高门架")) {
//                    wh += 14;
//                } else if (qitasheshijingchangjiancha.getField12().equals("洞口绿化")) {
//                    wh += 3;
//                } else if (qitasheshijingchangjiancha.getField12().equals("消音设施")) {
//                    wh += 3;
//                } else if (qitasheshijingchangjiancha.getField12().equals("减光设施")) {
//                    wh += 10;
//                } else if (qitasheshijingchangjiancha.getField12().equals("污水处理设施")) {
//                    wh += 4;
//                } else if (qitasheshijingchangjiancha.getField12().equals("洞口雕塑、隧道铭牌")) {
//                    wh += 2;
//                } else if (qitasheshijingchangjiancha.getField12().equals("房屋设施")) {
//                    wh += 35;
//                }
//            }
//        }
            //各项得分总和
            double dfzh = 0;
            System.out.println("这是第二步");
            for (BizQitasheshidingqijiancha qitasheshidingqijiancha: bizQitasheshidingqijianchaList) {
                if(bizQitasheshidingqijiancha.getField1().equals(bizQitasheshijishupingding.getField11())) {
                    double qtcii = Integer.parseInt(qitasheshidingqijiancha.getField15());   //分项设施状况值
                    if (qitasheshidingqijiancha.getField12().equals("电缆沟")) {
                        dfzh += (100 * qtcii)/ wh;
                        System.out.println(dfzh);
                    } else if (qitasheshidingqijiancha.getField12().equals("设备洞室")) {
                        dfzh += (100 * qtcii )/ wh;
                        System.out.println(dfzh);
                    } else if (qitasheshidingqijiancha.getField12().equals("洞外联络通道")) {
                        dfzh += (90 * qtcii)/ wh ;
                        System.out.println(dfzh);
                    } else if (qitasheshidingqijiancha.getField12().equals("洞口限高门架")) {
                        dfzh += 140* qtcii/ wh;
                    } else if (qitasheshidingqijiancha.getField12().equals("洞口绿化")) {
                        dfzh += 30  * qtcii/ wh;
                    } else if (qitasheshidingqijiancha.getField12().equals("消音设施")) {
                        dfzh += 30  * qtcii/ wh;
                    } else if (qitasheshidingqijiancha.getField12().equals("减光设施")) {
                        dfzh += 100  * qtcii/ wh;
                    } else if (qitasheshidingqijiancha.getField12().equals("污水处理设施")) {
                        dfzh += 40 * qtcii/ wh;
                    } else if (qitasheshidingqijiancha.getField12().equals("洞口雕塑、隧道铭牌")) {
                        dfzh += 20  * qtcii/ wh;
                    } else if (qitasheshidingqijiancha.getField12().equals("房屋设施")) {
                        dfzh += 350 * qtcii / wh;
                    }
                }
            }
            System.out.println("++++++++++++++++"+dfzh);
            //计算QTCI
            qtci =  (float)(100*(1-dfzh*0.05));
            //技术评定
            lev  = "无";
            if(qtci>=70){ lev = "1类";
            }else if(qtci>=40&& qtci<70){ lev = "2类";
            }else if(qtci<40){ lev = "3类";
            }

        }


        bizQitasheshijishupingding.setField8(qtci+"");   //QTCI
        bizQitasheshijishupingding.setField9(lev);   //技术评定等级

        BizQitasheshijishupingding bizQitasheshijishupingding2 =new BizQitasheshijishupingding();
        bizQitasheshijishupingding2.setField1(bizQitasheshijishupingding.getField1());
        bizQitasheshijishupingding.setField4("无");
        bizQitasheshijishupingding.setField5("无");
        bizQitasheshijishupingding.setField6(DateUtils.getDate());

        return toAjax(bizQitasheshijishupingdingService.insertBizQitasheshijishupingding(bizQitasheshijishupingding));
    }

    /**
     * 修改其他设施技术评定
     */
    @Log(title = "其他设施技术评定", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizQitasheshijishupingding bizQitasheshijishupingding)
    {
        return toAjax(bizQitasheshijishupingdingService.updateBizQitasheshijishupingding(bizQitasheshijishupingding));
    }

    /**
     * 删除其他设施技术评定
     */
//    @PreAuthorize("@ss.hasPermi('biz:qitasheshijishupingding:remove')")
    @Log(title = "其他设施技术评定", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bizQitasheshijishupingdingService.deleteBizQitasheshijishupingdingByIds(ids));
    }
}
